package view.item
{
	import control.manager.BitmapManager;
	import control.manager.LayerManager;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.geom.Point;
	
	import view.layer.GameLayer;
	import view.role.MapMonster;
	import view.role.MapObject;
	import view.role.MapPlayer;

	/**
	 * 攻击技能（普通攻击/技能） 
	 * @author leo
	 * 
	 */	
	public class NormalAtt extends ATT
	{
		private const SPEED :int = 15;
		private var startTarget :MapPlayer;
		private var endTarget :MapMonster;
		private var startPoint :Point;
		private var endPoint :Point;
		/**
		 * 攻击技能，包括普通攻击和释放的技能 
		 * @param startTarget
		 * @param endTarget
		 * @param type
		 * 
		 */		
		public function NormalAtt(startTarget :MapPlayer,endTarget :MapMonster,type :String = "normalATT")
		{
			super();
			
			var mc :MovieClip = BitmapManager.instance.getSkill(type);
			if(!mc)
			{
				trace("没有找到相应的技能动画");
				return;
			}
			this.addChild(mc);
			this.startTarget = startTarget;
			this.endTarget = endTarget;
			this.startPoint = startTarget.attStartPoint();
			this.x = startPoint.x;
			this.y = startPoint.y;
			this.endPoint = endTarget.attPosition;
		}
		
		override public function update():void
		{
			var targetX:Number = endPoint.x;
			var targetY:Number = endPoint.y;
			var distX:Number = targetX - this.x;
			var distY:Number = targetY - this.y;
			var forceSpeed :Point = new Point();
			
			var angle:Number = Math.atan2(distY, distX);
			forceSpeed.x = SPEED * Math.cos(angle);
			forceSpeed.y = SPEED * Math.sin(angle);
			
			//如果总路径小于总速度则表示已经抵达目的地，返回进行下一点行走
			if( Math.sqrt(distX * distX + distY * distY) < SPEED )
			{
				this.x = targetX;
				this.y = targetY;
				if(this.parent)
				{
					var gameLayer :GameLayer = LayerManager.instance.getLayer(GameLayer.NAME) as GameLayer;
					gameLayer.removeChild(this);
					gameLayer.att = null;
					this.endTarget.hurt(startTarget.playerData.playerATT);
				}
				return;
			}
			else
			{
				//如果还没抵达目的点，则继续行走
				this.x += forceSpeed.x;
				this.y += forceSpeed.y;
			}
		}
	}
}